package a51qindian.com.comprehensive.activity;

import android.widget.EditText;

import com.li.utils.ToastUtil;
import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.util.SpUtil;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;


/**
 * Author: Li
 * Date: 2017/10/13 0013
 * Description: 添加员工界面
 */

public class AddStaffActivity extends QDBaseActivity {


    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.et_staff_account)
    EditText mEtStaffAccount;
    @BindView(R.id.et_staff_pwd)
    EditText mEtStaffPwd;
    @BindView(R.id.et_pwd_a)
    EditText mEtPwdA;
    @BindView(R.id.et_real_name)
    EditText mEtRealName;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_staff;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
    }

    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {

        if (checkIsEmpty(mEtStaffAccount)||checkIsEmpty(mEtStaffPwd)
                ||checkIsEmpty(mEtPwdA)||checkIsEmpty(mEtRealName)) {
            showToast("请正确填写全部信息");
            return;
        }
        if (!mEtStaffPwd.getText().toString().equals(mEtPwdA.getText().toString())) {
            showToast("两次密码输入不一样");
            return;
        }
        addStaff(SpUtil.getUserInfo(AddStaffActivity.this, SpUtil.USER_ID),
                mEtStaffAccount.getText().toString(), mEtStaffPwd.getText().toString(),
                mEtPwdA.getText().toString(), mEtRealName.getText().toString());
    }

    /**
     * 添加员工
     *
     * @param uid      用户id
     * @param account  账号
     * @param pwd      密码
     * @param pwdA     确认密码
     * @param realName 真是姓名
     */
    private void addStaff(String uid, String account, String pwd, String pwdA, String realName) {
        mLayoutContent.onLoadingWithContent("正在添加员工");
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("username", account);
        map.put("password", pwd);
        map.put("repassword", pwdA);
        map.put("realname", realName);
        ObservableHelper.createObservable(RetrofitCreator.createService().addStaff(map))
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        mLayoutContent.onSuccess();
                        try {
                            JSONObject object=new JSONObject(s);
                            int code=object.getInt("code");
                            if (code!=1) {
                                ToastUtil.showToast(AddStaffActivity.this,object.getString("info"));
                            }else {
                                ToastUtil.showToast(AddStaffActivity.this,"添加成功");
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        ToastUtil.showToast(AddStaffActivity.this,e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
